import std.stdio;
import dlua.all;
import dlua.wrapper;
import std.variant;

static import clib = std.c.stdlib;
static import str = std.string;

extern (C) int showData(lua_State* L)
{
	writefln("------ Args using Lua ---------");
    auto context = Lua.Instance(L);
    foreach (int i, LuaVariable var; context.Arguments)
    {
        writefln("- Argument: [", str.toString(i), "] = ",
        var.Description);
    }
    foreach (string varname, LuaVariable var; context.Globals)
    {
        writefln("- Global:   [", varname, "] = ", var.Description);
        if (var.DataType == LuaType.Table)
        {
            printTable(var.TableValue, 1);
        }
    }

    return 1;
}

void printTable(LuaVariable[LuaVariable] tableVar, int tabCount)
{
    foreach (LuaVariable varname, LuaVariable var; tableVar)
    {
        writefln(str.repeat("\t", tabCount), "[", varname.StringValue, "] = ", var.Description);
        if (var.DataType == LuaType.Table)
        {
            printTable(var.TableValue, tabCount + 1);
        }
    }
}

void main()
{
    Lua lua = Lua.Instance();
    lua.Register("showData", &showData);    // function
    lua.Register("name", "komma8komma1");   // string
    lua.Register("age", 12);                // integer
    lua.Register("score", 85.23);           // double
    lua.Register("wealthy", false);         // boolean

    LuaVariable[LuaVariable] innerTable;
    innerTable[new LuaVariable("key")] = new LuaVariable("Rosebud");
    innerTable[new LuaVariable("id")] = new LuaVariable(332);

    LuaVariable[LuaVariable] table;
    table[new LuaVariable("x")] = new LuaVariable(12);
    table[new LuaVariable("y")] = new LuaVariable(-32);
    table[new LuaVariable("z")] = new LuaVariable(18.23);
    table[new LuaVariable("isEmpty")] = new LuaVariable(false);
    table[new LuaVariable("show")] = new LuaVariable(&showData);
    table[new LuaVariable("innerTable")] = new LuaVariable(innerTable);
    lua.Register("luatable", table);          // table

    lua.Execute(mySampleCode);

    lua.Execute("luafunction(75, 32)");
    writefln(lua.Globals["dman"].StringValue);

    lua.Register("doesVarAddWork", "indeed it does");
    lua.Execute(`print ("New variable is available in lua code? " .. doesVarAddWork)`);
    writefln("New variable is in the Globals collection? " ~ lua.Globals["doesVarAddWork"].StringValue);

    delete(lua);
}

string mySampleCode=`

-- sample comment
if name == "komma8komma1" then
	print ("Hello, " .. name .. "! I was hoping you could make it!")
else
	print ("Hello " .. name .. ", who the heck are you?")
end

numb = 0
dman="Walter Bright"

t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do print(k,v) end

function luafunction(x, y)
	print("X and Y: ", x, y);
end

luafunction(12, 14.3);

background = {r=0.30, g=0.10, b=0}

showData("test1", "test2", "test3", 12, false, 0.023, none, nil, background, showData, luafunction);

`;
